﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data.Stats
{
  public class StandardDeviationFunction : StatFunction
  {
    public StandardDeviationFunction()
      : base()
    {
    }

    public StandardDeviationFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    public StandardDeviationFunction( string resultPropertyName, string sourcePropertyName, bool entirePopulation )
      : base( resultPropertyName, sourcePropertyName )
    {
      m_entirePopulation = entirePopulation;
    }

    #region EntirePopulation Property

    public bool EntirePopulation
    {
      get
      {
        return m_entirePopulation;
      }
      set
      {
        this.CheckSealed();
        m_entirePopulation = value;
      }
    }

    private bool m_entirePopulation = false;

    #endregion

    #region PrerequisiteFunctions Property

    protected internal override StatFunction[] PrerequisiteFunctions
    {
      get
      {
        StatFunction[] prerequisites = m_prerequisites;

        if( prerequisites == null )
        {
          prerequisites = new StatFunction[]
          {
            new VarianceFunction( "StandardDeviationFunction.Variance", this.SourcePropertyName, m_entirePopulation )
          };

          if( this.IsSealed )
            m_prerequisites = prerequisites;
        }

        return prerequisites;
      }
    }

    #endregion

    #region RequiresAccumulation Property

    protected internal override bool RequiresAccumulation
    {
      get
      {
        return false;
      }
    }

    #endregion

    protected internal override void InitializeFromInstance( StatFunction source )
    {
      base.InitializeFromInstance( source );
      this.EntirePopulation = ( ( StandardDeviationFunction )source ).EntirePopulation;
    }

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected internal override void InitializePrerequisites( StatResult[] prerequisitesValues )
    {
      if( prerequisitesValues.Length != 1 )
        throw new InvalidOperationException( "The prerequisites initializers do not match the PrerequisiteFunctions." );

      m_variance = prerequisitesValues[ 0 ].Value;
    }

    protected internal override void Reset()
    {
      m_variance = null;
    }

    protected internal override void Accumulate( object[] values )
    {
      Debug.Assert( false, "Prerequisites are sufficient. Should not be called." );
    }

    protected internal override StatResult GetResult()
    {
      checked
      {
        return new StatResult( System.Math.Sqrt( Convert.ToDouble( m_variance ) ) );
      }
    }

    protected internal override int GetEquivalenceKey()
    {
      return base.GetEquivalenceKey() ^ m_entirePopulation.GetHashCode();
    }

    protected internal override bool IsEquivalent( StatFunction statFunction )
    {
      StandardDeviationFunction stdevFunction = statFunction as StandardDeviationFunction;

      if( stdevFunction == null )
        return false;

      if( stdevFunction.EntirePopulation != this.EntirePopulation )
        return false;

      return base.IsEquivalent( statFunction );
    }

    private StatFunction[] m_prerequisites; // = null
    private object m_variance;
  }
}
